package com.hrs.controller;

import java.text.SimpleDateFormat;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.poi.hpsf.Date;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hrs.pojo.User;
import com.hrs.service.UserService;
import com.sun.xml.bind.v2.schemagen.xmlschema.List;
@Controller
public class UserController {
	@Resource
	private UserService userService;
	//登入页面显示
	@RequestMapping("/login")
	public String loginShow(User user) {
		return "login";
	}
	//处理登入
	@RequestMapping("/loginDo")
	public String loginDo(User user,Model model,HttpServletRequest request) {
		User loginUser=userService.login(user);
		if(loginUser!=null) {
			HttpSession session=request.getSession();
			session.setAttribute("USER", loginUser);
			return "redirect:/index.do" ;
			
		}else {
			//登入失败
			//设置失败的错误信息
			model.addAttribute("user",user);
			model.addAttribute("errMsg","账号密码错误");
			
			return "forward:/login.do";
		}
	}
	@RequestMapping("/index")
	public String index() {
		return "index";
	}
	// 退出系统
	@RequestMapping("/exit")
	public String exit(HttpServletRequest request) {
		HttpSession session=request.getSession();
		session.invalidate();
		return "forward:/login.do";
	}
	//用户信息显示
	@RequestMapping("/userShow")
	public String userShow(String p,Model model) {
		PageHelper.startPage(Integer.parseInt(p),6);
		java.util.List<User> users=userService.selectAll();
		PageInfo<User> pageInfo=new PageInfo<>(users);
		model.addAttribute("pageInfo",pageInfo);
		return "userShow";
	}
	@RequestMapping("/userDelete")
	public String userDelete(String uid,String p) {
		userService.deleteUserByUid(Integer.parseInt(uid));
		return "redirect:/userShow.do?p="+p;
	}
	@RequestMapping("/userAdd")
	public String userAdd(User user) {
		return "userAdd";
	}
	@RequestMapping("/userAddDo")
	public String userAddDo(@Valid User user,BindingResult br,HttpServletRequest request) {
		if(br.hasErrors())
			return "userAdd";
		HttpSession session=request.getSession();
		User loginUser=(User)session.getAttribute("USER");
		user.setCreator(loginUser.getLoginname());
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		user.setCreatedate(sdf.format(new Date()));
		userService.insert(user);
		return "redirect:/userShow.do?p=1";
	}
	@RequestMapping("/userModify")
	public String userModify(Model model,String userid,String p) {
		User user=userService.selectUserByUid(Integer.parseInt(userid));
		model.addAttribute("user",user);
		model.addAttribute("p",p);
		return "userModify";
	}
	@RequestMapping("/userModifyDo")
	public String userModifyDo(@Valid User user,BindingResult br,HttpServletRequest request) {
		if(br.hasErrors()) {
			return "userModify";
		}
		User userTemp=userService.selectUserByUid(user.getUid());
		userTemp.setLoginname(user.getLoginname());
		userTemp.setLoginname(user.getPassword());
		userTemp.setLoginname(user.getStatus());
		userTemp.setLoginname(user.getUsername());
		userTemp.setLoginname(user.getRemark());
		userService.updateUserByUid(user);
		HttpSession session=request.getSession();
		String p=(String)session.getAttribute("PAGE_NUM");
		return "redirect:/userShow.do?p"+p;
	}
}
